var express = require('express'),
    router = express.Router(),
    tool = require('../utils/common'),
    userModel = require('../db/model/user'),
    user10Model = require('../db/model/user10_extend'),
    validate = require('../utils/validate');

/**
 * 获取用户信息
 */
router.get('/getInfo', (req, res) => {
    try {
        userModel.isLogin(req, res).then(result => {
            let auth = result;
            const params = req.query;
            console.log(typeof params, params);
            if (!params || !Object.keys(params).length) tool.throwError('参数错误');

            let model = new userModel();
            params.user_type = model.USER_TYPE_10;
            model.list(params, 1).then(result => {
                res.json(tool.returnArray(result, 200));
            }).catch(e => res.json(tool.returnArray(e)));
        }).catch(e => res.json(tool.returnArray(e)));
    } catch (e) {
        console.log(e);
        res.json(tool.errorHandle(e));
    }
});

/**
 * 修改密码
 */
router.post('/updatePwd', (req, res) => {
    (async (req, res) => {
        try {
            let auth = await userModel.isLogin(req, res);

            //获取参数，且验证参数
            const params = req.body, valid = new validate(), model = new userModel();
            if (!params || !Object.keys(params).length) tool.throwError('提交参数错误');
            params.pwd = params.pwd ? model.encryPwd(params.pwd) : '';
            params.opwd = auth.password;
            let validRes = valid.rules({
                'pwd': 'require|confirm:opwd',
                'npwd': 'require',
                'cnpwd': 'require|confirm:npwd'
            }).check(params);
            if (!validRes) tool.throwError(valid.getError());

            model.update({'password': model.encryPwd(params.npwd)}, {id: auth.id}).then(result => {
                res.json(tool.returnArray(result, 1));
            }).catch(e => res.json(tool.returnArray(e)));
        } catch (e) {
            res.json(tool.errorHandle(e));
        }
    })(req, res);
});

/**
 * 修改用户信息
 */
router.post('/updateInfo', (req, res) => {
    (async (req, res) => {
        try {
            let auth = await userModel.isLogin(req, res);

            //获取参数，且验证参数
            const params = req.body, valid = new validate();
            if (!params || !Object.keys(params).length) tool.throwError('提交参数错误');

            //基本信息保存
            let model = new userModel(), model10 = new user10Model(),
                result = await model.update({'nickname': ('nickname' in params ? params.nickname : '昵称')}, {'id': auth.id}).catch(e => false);
            result === false && tool.throwError('保存用户信息失败');

            //扩展信息保存
            let data10 = {
                'true_name': ('true_name' in params ? params.true_name : ''),
                'mobile': ('phone' in params ? params.phone : ''),
                'email': ('email' in params ? params.email : ''),
                'student_number': ('student_number' in params ? params.student_number : ''),
                'classs': ('classs' in params ? params.classs : '')
            }, user10List = await model10.list({'user_id': auth.id}, 1);
            if (user10List && user10List.length) {
                result10 = await model10.update(data10, {'user_id': auth.id}).catch(e => false);
            } else {
                data10.user_id = auth.id;
                result10 = await model10.add(data10).catch(e => false);
            }
            result === false && tool.throwError('保存用户扩展信息失败');

            res.json(tool.returnArray('success', 1));
        } catch (e) {
            console.log('e->', e);
            res.json(tool.errorHandle(e));
        }
    })(req, res);
});

/**
 * 提交意见
 */
router.post('/submitIdea', (req, res) => {
    try {
        userModel.isLogin(req, res).then(result => {
            let auth = result;
            const params = req.body;
            if (params.length) tool.throwError('参数错误');
            params.user_type = auth.user_type;
            params.user_id = auth.id;

            let ideaModel = require('../db/model/idea'), model = new ideaModel();
            model.add(params).then(result => {
                res.json(tool.returnArray(result, 200));
            }).catch(e => res.json(tool.returnArray(e)));
        }).catch(e => res.json(tool.returnArray(e)));
    } catch (e) {
        res.json(tool.errorHandle(e));
    }
});

module.exports = router;
